package com.example.usermanager.config;

import cn.hutool.core.lang.hash.Hash;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.HashMap;

/**
 * @author 郝文雪
 * @date 2022/8/6 10:22
 * @Description 返回统一的数据格式
 */

@ControllerAdvice
public class MyResopnseAdvice implements ResponseBodyAdvice {
    /**
     * 返回true表示进行下面的重写方法
     * 返回false表示不再继续执行下面的beforeBodyWrite方法
     * @param returnType
     * @param converterType
     * @return
     */
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        HashMap<String,Object> result=new HashMap<>();
        result.put("state",1);
        result.put("msg","");
        result.put("data",body);
        return result;
    }
}
